package com.example.maven;
import java.sql.*;

public class Test {

    public static final String URL="jdbc:mysql://localhost:3306/wuye?characterEncoding=utf8&serverTimezone=UTC";
    public static final String NAME="root";
    public static final String PASSWORD="123456";
    public static void main(String[] args) {
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            //注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //建立连接
            conn= DriverManager.getConnection(URL,NAME,PASSWORD);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        try {
            //获取执行对象
            ps= conn.prepareStatement("select * from user");
            //获取结果集
            rs= ps.executeQuery();
            //遍历结果集
            while (rs.next()){
                System.out.println(rs.getString("username"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        //关闭连接池
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

